package com.example.bgdharaniPlayer.ui;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.bgdharaniPlayer.R;
import com.example.bgdharaniPlayer.service.MusicService;

public class SettingsActivity extends AppCompatActivity {

    private SeekBar seekBarVolume;
    private TextView tvVolumeValue;
    private Switch switchAutoStart;
    private Button btnSaveSettings;

    private MusicService musicService;
    private boolean isBound = false;
    private SharedPreferences sharedPreferences;

    private final ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
            musicService = binder.getService();
            isBound = true;
            
            // 更新音量设置
            seekBarVolume.setProgress((int)(musicService.getVolume() * 100));
            tvVolumeValue.setText(String.format("%d%%", (int)(musicService.getVolume() * 100)));
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicService = null;
            isBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        sharedPreferences = getSharedPreferences("AppSettings", Context.MODE_PRIVATE);

        initViews();
        setupListeners();
        loadSettings();
        bindMusicService();
    }

    private void initViews() {
        seekBarVolume = findViewById(R.id.seekBarSettingsVolume);
        tvVolumeValue = findViewById(R.id.tvVolumeValue);
        switchAutoStart = findViewById(R.id.switchAutoStart);
        btnSaveSettings = findViewById(R.id.btnSaveSettings);
    }

    private void setupListeners() {
        seekBarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvVolumeValue.setText(String.format("%d%%", progress));
                if (isBound && fromUser) {
                    float volume = progress / 100f;
                    musicService.setVolume(volume);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 不需要实现
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 不需要实现
            }
        });

        btnSaveSettings.setOnClickListener(v -> {
            saveSettings();
            finish();
        });
    }

    private void bindMusicService() {
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private void loadSettings() {
        boolean autoStart = sharedPreferences.getBoolean("autoStart", false);
        switchAutoStart.setChecked(autoStart);
    }

    private void saveSettings() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("autoStart", switchAutoStart.isChecked());
        editor.putFloat("volume", seekBarVolume.getProgress() / 100f);
        editor.apply();
    }

    @Override
    protected void onDestroy() {
        if (isBound) {
            unbindService(serviceConnection);
            isBound = false;
        }
        super.onDestroy();
    }
}